<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity {

    /**
     * User's attributes
     * @var array
     */
    public $user;
    private $_id;

    public function authenticate() {
        $this->errorCode = self::ERROR_PASSWORD_INVALID;
        $user = User::model()->findByAttributes(array('username' => CHtml::encode($this->username)));

        if ($user) {
            if ($user->password === $this->password) {
                //if ($user->password === md5($user->salt.$this->password)) {
                $this->errorCode = self::ERROR_NONE;
                $user->save();
                $this->_id = $user->id;
                $this->setUser($user);
            }
        }

        unset($user);
        return!$this->errorCode;
    }

    public function getId() {

        return $this->_id;
    }

    public function getUser() {
        return $this->user;
    }

    public function setUser(CActiveRecord $user) {
        $this->user = $user->attributes;
    }

}

?>